package com.dfk.bs.controller.admin;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dfk.bs.common.constants.ResultCodeEnum;
import com.dfk.bs.common.exception.BsException;
import com.dfk.bs.common.vo.R;
import com.dfk.bs.dao.admin.TimeselectMapper;
import com.dfk.bs.entity.admin.Admin;
import com.dfk.bs.entity.admin.Timeselect;
import com.dfk.bs.service.admin.AdminService;
import com.dfk.bs.service.admin.TimeselectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @author lxr
 * @email lixuerong@jsgnjf.com.cn
 * @date 2019-12-18 10:10:25
 */

@Api("用电器电器管理")
@CrossOrigin //跨域
@RestController
@RequestMapping("/ddkbishe/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @Autowired
    private TimeselectService timeselectService;

    @Resource
    private TimeselectMapper timeselectMapper;


    @ApiOperation("单个管理员信息")
    @PostMapping("/listOne")
    public R listOne(
            @ApiParam(name = "username", value = "管理员姓名", required = true)
            @RequestBody JSONObject json
            ) {

        String username = json.getString("username");
        Admin one = adminService.getOne(Wrappers.<Admin>lambdaQuery()
                .eq(Admin::getUsername, username), false);
        //管理员查询更新密码记录

        System.out.println("username"+username);
        List<Timeselect> listfive = timeselectMapper.listfive(username);


        return R.ok()
                .message("查询单个管理员信息成功")
                .data("items", one)
                .data("items2",listfive);
    }

    @ApiOperation("新增管理员")
    @PostMapping("/save")
    public R save(
            @ApiParam(name = "admin", value = "管理员基本信息", required = true)
            @RequestBody Admin admin) {
        adminService.save(admin);
        return R.ok()
                .message("新增管理员成功");
    }

    @ApiOperation("根据id删除管理员")
    @DeleteMapping("/remove/{id}")
    public R removeById(
            @ApiParam(name = "id", value = "id", required = true)
            @PathVariable String id) {
        adminService.removeById(id);

        return R.ok()
                .message("删除管理员成功");
    }

    @ApiOperation("分页条件查询管理员列表")
    @PostMapping("/pageQuery")
    public R pageQuery(@RequestBody Map<String, Object> params) {

        Integer page = (Integer) params.get("page");
        Integer limit = (Integer) params.get("limit");

        String selectname = (String) params.get("selectname");
        //业务逻辑错误，抛出自定义异常
        if (page <= 0 || limit <= 0) {
            throw new BsException(ResultCodeEnum.PARAM_ERROR);
        }
        Page<Admin> pageParam = new Page<>(page, limit);

        /*已经把查询的每一页的信息放到 pageParam中*/
        adminService.pageQuery(pageParam, selectname);

        List<Admin> records = pageParam.getRecords();
        long total = pageParam.getTotal();

        return R.ok()
                .message("分页条件查询管理员列表成功")
                .data("total", total)
                .data("rows", records);
    }

    @ApiOperation(value = "根据ID修改管理员")
    @PutMapping("/update")
    public R updateById(
            @ApiParam(name = "admin", value = "管理员对象", required = true)
            @RequestBody Admin admin) {
          adminService.updateById(admin);
        //跟新密码的时候插入
        Timeselect timeselect=new Timeselect();
        timeselect.setUpdatetime(admin.getUpdatetime());
        timeselect.setUsername(admin.getUsername());
        timeselectService.save(timeselect);

        return R.ok()
                .message("修改管理员成功");
    }



}
